﻿using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Xml;
using System.Collections.Generic;
using System.IO;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    XmlTextReader rssReader;
    XmlDocument rssDoc;
    XmlNode nodeRss;
    XmlNode nodeChannel;
    XmlNode nodeItem;
    string fs;

    XmlDataDocument xmldoc = new XmlDataDocument();
    XmlNodeList xmlnode;
   // FileStream fs = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Cities.xml"), FileMode.Open, FileAccess.Read);
    
    public Service () {
       
    }

    private void getCity()
    {
        fs = Server.MapPath("Cities.xml");
        xmldoc.Load(fs);
        xmlnode = xmldoc.GetElementsByTagName("City");
    }

    [WebMethod]
    public List<string> ListCity()
    {
        getCity();
        List<string> Items = new List<string>();
        for (int i = 0; i < xmlnode.Count; i++)
        {
            Items.Add(xmlnode[i].ChildNodes.Item(0).InnerText);
        }
        return Items;
    }

    [WebMethod]
    public bool LoadWeatherXML(string name)
    {
        getCity();
        string url = "http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=ASI|VN|VM060|HO%20CHI%20MINH%20CITY";
        for(int i=0; i< xmlnode.Count ; i++)
            if (xmlnode[i].ChildNodes.Item(0).InnerText == name)
            {
                url = xmlnode[i].ChildNodes.Item(1).InnerText;
            }
        rssReader = new XmlTextReader(url);
        rssDoc = new XmlDocument();
        try
        {
            rssDoc.Load(rssReader);
        }
        catch
        {
            return false;
        }
        return true;
    }

    [WebMethod]
    public List<string> ListCityInfo(string cityName)
    {
        List<string> Info = new List<string>();
        if (LoadWeatherXML(cityName) == true)
        {
            for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
            {
                if (rssDoc.ChildNodes[i].Name == "rss")
                {
                    nodeRss = rssDoc.ChildNodes[i];
                }
            }

            for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
            {
                if (nodeRss.ChildNodes[i].Name == "channel")
                {
                    nodeChannel = nodeRss.ChildNodes[i];
                    Info.Add(nodeChannel["title"].InnerText);
                    Info.Add(nodeChannel["language"].InnerText);
                    Info.Add(nodeChannel["link"].InnerText);
                    Info.Add(nodeChannel["description"].InnerText);
                }
            }
            
        }
        return Info;
    }

    [WebMethod]
    public List<string> ListItems(string nameCity)
    {
        List<string> Items = new List<string>();
        if (ListCityInfo(nameCity) != null)
        {
            for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
            {
                if (nodeChannel.ChildNodes[i].Name == "item")
                {
                    nodeItem = nodeChannel.ChildNodes[i];
                    Items.Add(nodeItem["title"].InnerText);
                    Items.Add(nodeItem["link"].InnerText);
                    Items.Add(nodeItem["pubDate"].InnerText);
                    Items.Add(nodeItem["description"].InnerText);
                }
            }
        }
        return Items;
    }
}
